/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Graphics2D;
import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;

/**
 *
 * @author Jacob Morgan
 */
public class EFrame extends EComponent{
    protected Color backgroundColor= new Color(62, 67, 77);
    protected Color borderColor = new Color(80, 80, 80);
    protected Color shadowColor = new Color(0, 0, 0, 64);
    protected boolean undecorated = false;
    public EFrame(EComponent parent, int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        if(layer == -1)
            layer = 1;
        if(parent != null)
            parent.addChild(layer, this);
    }
    public EFrame(int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    public EFrame(EComponent centerIn, int width, int height)
    {
        this.width = width;
        this.height = height;
        this.x = (centerIn.getWidth()/2) - width/2;
        this.y = (centerIn.getHeight()/2) - height/2;
    }
    public EFrame(int width, int height)
    {
        this.x = 0;
        this.y = 0;
        this.width = width;
        this.height = height;
        isPrimary = true;
    }
    public void setUndecorated(boolean v)
    {
        undecorated = v;
    }
    public void addTitle(String text)
    {
        addTitle(text, null);
    }
    public void addTitle(String text, final Runnable closeButtonAction)
    {
        EStickyFrame f = new EStickyFrame(this, -1, 20, 3, 3, 3, 0);
        ELabel l = new ELabel(text);
        l.setFont(EDGESettings.menuFont);
        l.setHeight(20);
        l.setX(3);
        l.setY(0);
        f.addChild(1, l);
        f.setUndecorated(true);
        f.setBackgroundColor(new Color(127, 136, 154));
        EStickyFrame closeFrame = new EStickyFrame(f, 20, 20, 0, -1, 1, 0);
        closeFrame.setUndecorated(true);
        EButton close = new EButton("X");
        close.setY(-1);
        close.setWidth(20);
        close.setHeight(20);
        close.setActionListener(new EActionListener() {

            @Override
            public void actionPerformed() {
                visible = false;
                if(closeButtonAction != null)
                    closeButtonAction.run();
            }
        });
        closeFrame.addChild(1, close);
    }
    public void setBackgroundColor(Color c)
    {
        backgroundColor = c;
    }
    public void setBorderColor(Color c)
    {
        borderColor = c;
    }
    public EComponent getChildAt(int px, int py)
    {
        for(int i = 7; i >= 0; i--)
        {
            for(EComponent c : getChildren(i))
            {
                if(c.testPoint(px, py) && c.visible && c.enabled)
                    return c;
            }
        }
        return null;
    }
    @Override
    public void mousePressed()
    {
        beingDragged = true;
    }
    public void mouseUp()
    {
        beingDragged = false;
    }
    @Override
    public boolean mouseDrag(int x, int y, int px, int py)
    {
        boolean r = super.mouseDrag(x, y, px, py);
        if(fixed == false && r)
        {
            this.x += (x - px);
            this.y += (y - py);
        }
        return false;
    }
    @Override
    public void render(Graphics2D g)
    {
        super.render(g);
        if(this instanceof EMenu)
            isPrimary = false;
        if(!isPrimary)
        {
            int w = getWidth();
            int h = getHeight();
            if(undecorated)
            {
                g.setColor(backgroundColor);
                g.fillRect(getX(), getY(), w, h);
            }
            else
            {
                g.setColor(shadowColor);
                g.fillRect(getX() + 3, getY() + 3, w, h);
                g.setColor(backgroundColor);
                g.fillRect(getX(), getY(), w, h);
                if(borderColor != null)
                {
                    g.setColor(borderColor);
                    g.drawRect(getX() + 2, getY() + 2, w - 5, h - 5);
                }
                g.setColor(shadowColor);
            }
        }
        super.render(g);
    }
}
